package com.tomcat;

import com.servlet.HttpServlet;

import java.net.Socket;

public class TomCatThread implements Runnable{
    private Socket socket;

    public TomCatThread(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        HttpRequest httpRequest = new HttpRequest(socket);
        HttpResponse httpResponse = new HttpResponse(socket);

        String requestURI = httpRequest.getRequestURI();
        // 判断请求的是Servlet 还是静态资源
        int indexOfPoint = requestURI.indexOf(".");
        if (indexOfPoint == -1) { // 请求的是Servlet
            // 判断是否已经创建了此Servlet实例
            if (TomCat.getConcurrentMappingHashMap().containsKey(requestURI)) {
                String className = TomCat.getConcurrentMappingHashMap().get(requestURI);
                if (TomCat.getConcurrentServletHashMap().containsKey(className)) { // HashMap中已经实例化此Servlet
                    HttpServlet httpServlet = TomCat.getConcurrentServletHashMap().get(className);
                    httpServlet.service(httpRequest, httpResponse);
                } else { // HashMap中没有实例化此Servlet
                    // 判断web.xml文件中是否有此Servlet声明
                    // 此处直接默认请求的此Servlet不存在
                    // 直接返回Not Found
                    httpResponse.writer("Not Found!!!");
                }
            } else {
                httpResponse.writer("Not Found!!!");
            }
        } else { // 请求的是静态资源
            // 暂时不处理

        }



    }
}
